package com.jw.blog.wblog.controller.admin;

import com.jw.blog.wblog.entity.custom.ResultBean;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * @author wan Email-wlf18726994755@163.com
 * @version 1.0
 * @className FileBrowseUtil
 * @date 2020/6/21 15:06
 **/
@RestController
public class FileBrowseUtil {


    @PostMapping("/admin/getFileList")
    public ResultBean<ArrayList<String>> calculateGeoServlet(HttpServletRequest request) {
        ResultBean<ArrayList<String>> resultBean = new ResultBean<>();
        String params = "upload";
        ArrayList<String> list = new ArrayList<>();

        ArrayList<String> files = getFiles(params);


        String parent = request.getScheme()+"://"+request.getServerName()
                +":"+request.getServerPort()+"/upload/";
        System.out.println("domain" + parent);

        if (files.size() > 0) {
            resultBean.setCode(ResultBean.SUCCESS);
            for (String file : files) {
                list.add(parent + file);
            }
            resultBean.setData(list);
        } else {
            resultBean.setCode(ResultBean.NO_PERMISSION);
        }
        return resultBean;
    }


    /**
     * 删除封面图片
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/admin/deletePic")
    public ResultBean<Object> deletePic(String path, HttpServletRequest request) {
        ResultBean<Object> resultBean = new ResultBean<>();
        String[] split = path.split("/");
        String tempPath = split[split.length - 1];

        //获取当前项目的上一级路径
        String s = System.getProperty("user.dir");
        //获取当前项目名
        String contextPath = request.getContextPath();
        //上传文件的路径
        String filePath = s + "/" + contextPath + "/upload/" + tempPath;

        File fileTemp = new File(filePath);
        if (fileTemp.exists()) {
            if (fileTemp.isFile()) {
                if (fileTemp.delete()) {
                    resultBean.setCode(ResultBean.SUCCESS);
                    resultBean.setMsg("删除成功");
                } else {
                    // 0 表示删除失败
                    resultBean.setCode(ResultBean.FAIL);
                    resultBean.setMsg("删除失败");
                }
            }
        }
        return resultBean;
    }


    /**
     * 通过递归得到某一路径下所有的目录及其文件
     *
     * @param filePath 文件路径
     * @return f返回一个
     */
    public static ArrayList<String> getFiles(String filePath) {
        ArrayList<String> strings = new ArrayList<>();
        File file = new File(filePath);
        String[] list = file.list();
        if (list != null) {
            strings.addAll(Arrays.asList(list));
        }
        System.out.println(strings);
        return strings;
    }
}
